var SZModel           = require('./SZModel');
var SZProto           = require('./SZProto');
var SZLogic           = require('./SZLogic');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,
	properties: {
	},

	onLoad: function () {
		this.cardsNode = cc.find('cards', this.node);
		this.cardTypeNode = cc.find('typebg', this.node);
		this.stateNode = cc.find('state', this.node);
		this.cardsNode.active = false;
		this.cardTypeNode.active = false; 
		this.stateNode.active = false;

		this.cardsNodeArray = [];
		this.cardsNodePosArray = [];
		for (let i = 1; i <= 5; i+=2) {
			let node = this.cardsNode.getChildByName('card'+i);
			this.cardsNodeArray.push(node);
			this.cardsNodePosArray.push(node.getPosition());
		}
		this.cardTypeNodePos = this.cardTypeNode.getPosition();

		Global.MessageCallback.addListener('RoomMessagePush', this);
		Global.MessageCallback.addListener('GameMessagePush', this);
		Global.MessageCallback.addListener('ReConnectSuccess', this);
		Global.MessageCallback.addListener('UpdateGameCardBg', this);
	},

	setIndex: function (index) {
		this.index = index;
	},

	getChairID: function () {
		if (SZModel.getMyChairID() >= SZModel.getChairCount()) {
			return this.index;
		}
		else {
			return (SZModel.getMyChairID()+this.index)%SZModel.getChairCount();
		}
	},

	gameInit () {
		console.log("SZCards gameInit");
		if (typeof(this.index) != 'number') { return; }
		let cards = SZModel.getHandCardsByChairID(this.getChairID());
		this.cardsNode.active = false;
		this.cardTypeNode.active = false;
		if (cards) {
			for (let i = 0; i < this.cardsNodeArray.length; ++i) {
				this.cardsNodeArray[i].setPosition(this.cardsNodePosArray[i]);
				this.cardsNodeArray[i].setScale(1);
			}
			this.node.active = true;
			this.sendCards();
			let canShowType = true;
			for (let i = 0; i < cards.length; ++i) {
				if (cards[i] == 0) {
					canShowType = false;
				}
			}
			if (canShowType) {
				this.showType();
			}
		}
		this.updateStateSprite();
	},

	updateStateSprite: function () {
		let userStatus = SZModel.getUserStatusByChairID(this.getChairID());
		if (userStatus == 0 || SZModel.getGameStatus() == SZProto.gameStatus.RESULT) {
			this.stateNode.active = false;
			return;
		}
		this.stateNode.active = true;
		let url = 'Game/Sanzhang/';
		if ((userStatus&SZProto.userStatus.LOSE) > 0) {
			url += 'bipaishibai';
		}
		else if ((userStatus&SZProto.userStatus.TIMEOUTABANDON) > 0) {
			/* url += 'chaoshiqipai'; */
			url += 'yiqipai';
		}
		else if ((userStatus&SZProto.userStatus.ABANDON) > 0) {
			url += 'yiqipai';
		}
		else if ((userStatus&SZProto.userStatus.LOOK) > 0) {
			url += 'yikanpai';
			if (this.getChairID() == SZModel.getMyChairID()) {
				this.stateNode.active = false;
			}
		}
		Global.CCHelper.updateSpriteFrame(url, this.stateNode.getComponent(cc.Sprite));
	},

	messageCallbackHandler: function(router, msg) {
		if (!SZModel.getGameInited()) { return; }
		if (router === 'RoomMessagePush') {
			if(msg.type === RoomProto.GET_ROOM_SCENE_INFO_PUSH) {
				this.gameInit();
			}
			else if(msg.type === RoomProto.USER_LEAVE_ROOM_RESPONSE) {
				/* if(msg.data.chairID == this.getChairID()) {
					this.node.active = false;
				} */
			}
			else if(msg.type === RoomProto.OTHER_USER_ENTRY_ROOM_PUSH) {
				if (msg.data.roomUserInfo.chairID == this.getChairID()) {
					this.gameInit();
				}
			}
			else if(msg.type === RoomProto.USER_LEAVE_ROOM_PUSH) {
				if (msg.data.roomUserInfo.chairID == this.getChairID()) {
					this.node.active = false;
				}
			}
			else if(msg.type === RoomProto.USER_CHANGE_SEAT_PUSH) {
				this.gameInit();
			}
			else if(msg.type === RoomProto.USER_READY_PUSH) {
				if (msg.data.chairID == SZModel.getMyChairID()) {
					this.cardsNode.active = false;
					this.cardTypeNode.active = false;
				}
			}
		}
		else if(router === 'GameMessagePush') {
			if(msg.type === SZProto.GAME_LOOK_PUSH) {
				if (this.getChairID() == msg.data.chairID) {
					if (!msg.data.cuopai) {
						this.sendCards();
						this.showType(true);
						this.updateStateSprite();
					}
					else {
						this.showTwoCards();
					}
				}
			}
			else if(msg.type === SZProto.GAME_SEND_CARDS_PUSH) {
				this.sendCards(true);
			}
			else if(msg.type === SZProto.GAME_STATUS_PUSH) {
				if (msg.data.gameStatus == SZProto.gameStatus.SEND_CARDS) {
					this.cardsNode.active = false;
					this.cardTypeNode.active = false;
				}
			}
			else if(msg.type === SZProto.GAME_ABANDON_PUSH) {
				if (msg.data.chairID == this.getChairID()) {
					this.updateStateSprite();
				}
			}
			else if(msg.type === SZProto.GAME_COMPARE_PUSH) {
				if (msg.data.loseChairID == this.getChairID()) {
					this.updateStateSprite();
				}
			}
			else if(msg.type === SZProto.GAME_RESULT_PUSH) {
				this.sendCards();
				this.showType(true);
			}
			else if(msg.type === SZProto.GAME_MINGPAI_PUSH) {
				this.sendCards();
				this.showType(true);
			}
		} 
		else if(router === 'UpdateGameCardBg') {
			this.sendCards(); 
		}
	},

	getCard0Url: function () {
		let cardbg = cc.sys.localStorage.getItem('cardBg');
		let url = 'Game/Common/card_back_0';
		if (cardbg == 'cardBg1') {
			url = 'Game/Common/card_back_0';
		}
		else if (cardbg == 'cardBg2') {
			url = 'Game/Common/card_back_1';
		}
		else if (cardbg == 'cardBg3') {
			url = 'Game/Common/card_back_2';
		}
		else if (cardbg == 'cardBg4') {
			url = 'Game/Common/card_back_3';
		}
		return url;
	},

	sendCards: function (animal) {
		let cards = SZModel.getHandCardsByChairID(this.getChairID());
		if (cards == null) { return; }
		for (let i = 0; i < this.cardsNodeArray.length; ++i) {
			let url = 'Game/Cards/'+cards[i];
			if (cards[i] == 0) { url = this.getCard0Url(); }
			Global.CCHelper.updateSpriteFrame(url, this.cardsNodeArray[i].getComponent(cc.Sprite));
			this.cardsNodeArray[i].stopAllActions();
			this.cardsNodeArray[i].setPosition(this.cardsNodePosArray[i]);
		}
		this.cardsNode.active = true;
		this.node.active = true;
		if (animal) {
			let node_pos = this.node.getPosition();
			let audio_func = cc.callFunc(() => {
				Global.AudioManager.playSound('Game/Common/Audio/foldpai');
			});
			for (let i = 0; i < this.cardsNodeArray.length; ++i) {
				let card_node = this.cardsNodeArray[i];
				let card_node_pos = card_node.getPosition();
				card_node.setPosition(cc.v2(
					card_node_pos.x-node_pos.x,
					card_node_pos.y-node_pos.y
				));
				card_node.setScale(0);
				card_node.runAction(cc.sequence(
					cc.delayTime(0.1*i),
					cc.moveTo(0.2, this.cardsNodePosArray[i]),
					audio_func
				));
				card_node.runAction(cc.sequence(
					cc.delayTime(0.1*i),
					cc.scaleTo(0.2, 1)
				));
			}
		}
	},

	showTwoCards: function () {
		let cards = SZModel.getHandCardsByChairID(this.getChairID());
		if (cards == null) { return; }
		for (let i = 0; i < this.cardsNodeArray.length-1; ++i) {
			Global.CCHelper.updateSpriteFrame('Game/Cards/'+cards[i], this.cardsNodeArray[i].getComponent(cc.Sprite));
		}
	},

	showType: function (animal) {
		this.stateNode.active = false;
		let cards = SZModel.getHandCardsByChairID(this.getChairID());
		if (!cards || cards[0]+cards[1]+cards[2] == 0) {
			return;
		}
		this.cardTypeNode.active = true;
		let type = SZLogic.getCardsType(cards);
		let url = 'Game/Sanzhang/';
		let array = ['', 'danzhang', 'duizi', 'shunzi', 'jinhua', 'shunjin', 'baozi'];
		url += array[type];

		let audio = 'Game/Sanzhang/Audio/woman/';
		let user = SZModel.getUserByChairID(this.getChairID());
		if (user && user.userInfo.sex == 0) {
			audio = audio.replace(/woman/, "man");
		}
		let audios = ['', '', 'paiType1', 'paiType2', 'paiType3', 'paiType4', 'paiType5'];
		
		let typeNode = this.cardTypeNode.getChildByName('type');
		Global.CCHelper.updateSpriteFrame(url, typeNode.getComponent(cc.Sprite));
		this.cardTypeNode.stopAllActions();
		this.cardTypeNode.setPosition(this.cardTypeNodePos);
		if (animal) {
			let x = this.cardTypeNodePos.x;
			let y = this.cardTypeNodePos.y;
			let offx = (this.index == 0)? 125:50;
			this.cardTypeNode.x = x+offx;
			this.cardTypeNode.stopAllActions();
			this.cardTypeNode.runAction(cc.moveTo(0.2, cc.v2(x, y)).easing(cc.easeIn(3.0)));
			if (audios[type].length > 0) {
				audio += audios[type]; 
				this.scheduleOnce(() => {
					Global.AudioManager.playSound(audio);
				}, 0.5);
			}
		}
	},

	onDestroy: function () {
		Global.MessageCallback.removeListener('RoomMessagePush', this);
		Global.MessageCallback.removeListener('GameMessagePush', this);
		Global.MessageCallback.removeListener('ReConnectSuccess', this);
		Global.MessageCallback.removeListener('UpdateGameCardBg', this);
	},
});

